<?php
/**
 * Created by Chukintang on 2018/5/21 16:55.
 */
namespace app\common\validate;
use app\common\model\Admin;
use utils\PwdUtil;
use utils\RandomStringGenerator;

class AdminValidate extends CommonValidate{
    protected $rule = [
        'admin_name'=>'require|alphaNum|length:4,16',
        'old_pwd'=>'require|alphaDash|length:4,16|checkCorrect',
        'pwd'=>'require|alphaDash|length:4,16',
        're_pwd'=>'require|alphaDash|length:4,16',

    ];

    protected $message = [
        'admin_name'=>'管理员帐号必须是长度为4-16位的数字字母组合',
        'old_pwd'=>'原密码错误',
        'pwd'=>'管理员密码必须是长度为6-16位的数字,字母,下划线,破折号组合',
        're_pwd'=>'管理员密码必须是长度为6-16位的数字,字母,下划线,破折号组合',
    ];

    protected $scene = [
        'editPwd' => ['admin_name', 'old_pwd','pwd', 're_pwd'],
    ];

    protected function checkCorrect($value, $rule, $data, $field, $title){
        $info = Admin::get(['id'=>$data['id']]);
        if($info['pwd'] != PwdUtil::encode($data['old_pwd'].$info['salt'])){
            return '原密码错误';
        }
        return true;
    }
}

